package com.eboss.sbs.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.BaseActivity;
import com.eboss.sbs.adapter.ChooseOptionAdapter;
import com.eboss.sbs.adapter.ReceiptInfoListAdapter;
import com.eboss.sbs.enums.EnumConsts;
import com.eboss.sbs.model.db.OrderDetail;
import com.mytools.tool.utils.StringUtil;
import com.mytools.tool.utils.TimeUtil;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Date;
import java.util.List;

public class ReceiptInfoActivity extends BaseActivity implements OnItemClickListener {

	private int TYPE_PAY_OPTION = 2;
	
	private final int REQUEST_CODE_QUERYDATE_FROM = 1;
	private final int REQUEST_CODE_QUERYDATE_TO = 2;
	
	private ListView listview;
	private ReceiptInfoListAdapter adapter;
	private List<OrderDetail> datas;
	
	private Date panel_dateFrom = new Date();
	private Date panel_dateTo = new Date();
	private Date query_dateFrom = panel_dateFrom;
	private Date query_dateTo = panel_dateTo;
	
	private AlertDialog dialog_panel;
	
	//日期选择
	private TextView tv_date_to, tv_date_from;
	
	//支付方式
	private GridView gv_option_paytype;
	private ArrayList<String> paytype_values = new ArrayList<String>(){
		{
			add(EnumConsts.SqbPayChannel.ALL.getName());
			add(EnumConsts.SqbPayChannel.CARD_MEMO.getName());
			add(EnumConsts.SqbPayChannel.CASH_MEMO.getName());
			add(EnumConsts.SqbPayChannel.WEIXIN.getName());
			add(EnumConsts.SqbPayChannel.ALIPAY.getName());
			add(EnumConsts.SqbPayChannel.WALLET.getName());
		}
	};
	private BitSet paytype_marks = new BitSet(paytype_values.size());
	private ChooseOptionAdapter adapter_paytype;
	private int payType = EnumConsts.SqbPayChannel.ALL.getType();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_receipt_info);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, true);
		setTvText(R.id.tv_title, "收款数据 ");
		showView(R.id.next_sure, true);
		setTvText(R.id.next_sure, "筛选");
		
		String from = TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日");
		setTvText(R.id.tv_date_choose, from);
		
		datas = new ArrayList<OrderDetail>();
		listview = (ListView)findViewById(R.id.listview);
		listview.setOnItemClickListener(this);
		adapter = new ReceiptInfoListAdapter(this, datas);
		
		findViewById(R.id.titleback_btn_back).setOnClickListener(this);
		findViewById(R.id.next_sure).setOnClickListener(this);
		findViewById(R.id.ly_previous).setOnClickListener(this);
		findViewById(R.id.ly_next).setOnClickListener(this);
		
		listview.setAdapter(adapter);
		showNextDayBtn();
		
		getdata();
	}
	
	private void getdata(){
		
		long from = TimeUtil.getProtoDate(panel_dateFrom).getTime();
		long to = TimeUtil.getProtoDate(TimeUtil.getDateFromDate(query_dateTo, 1)).getTime();
		datas.clear();
		
		String where = "tradeTime>="+from+" and tradeTime<"+to;
		if(payType!= EnumConsts.SqbPayChannel.ALL.getType()){where = where + " and payType = "+payType;}
		
		List<OrderDetail> result_datas = DataSupport
		.where(where)
		.order("tradeTime desc")
		.find(OrderDetail.class);
		
		int num = 0;
		int money = 0;
		if(result_datas!=null&&result_datas.size()>0){
			num = result_datas.size();
			datas.addAll(result_datas);
		}
		for (OrderDetail orderDetail : result_datas) {
			money = money + orderDetail.getTradeMoney();
		}
		setTvText(R.id.tv_trade_num, num+"笔");
		setTvText(R.id.tv_trade_money, StringUtil.priceShow(money)+"元");
		
		adapter.notifyDataSetChanged();
	}
	
	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		switch (v.getId()) {
			case R.id.titleback_btn_back:
				finish();
				break;
			case R.id.next_sure:
				showPanel();
				break;
			case R.id.ly_previous:
				query_dateFrom = TimeUtil.getDateFromDate(query_dateFrom, -1);
				query_dateTo = query_dateFrom;
				panel_dateFrom = query_dateFrom;
				panel_dateTo = query_dateTo;
				payType = EnumConsts.SqbPayChannel.ALL.getType();
				setTvText(R.id.tv_date_choose, TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日"));
				showNextDayBtn();
				getdata();
				break;
			case R.id.ly_next:
				query_dateFrom = TimeUtil.getDateFromDate(query_dateFrom, 1);
				query_dateTo = query_dateFrom;
				panel_dateFrom = query_dateFrom;
				panel_dateTo = query_dateTo;
				payType = EnumConsts.SqbPayChannel.ALL.getType();
				setTvText(R.id.tv_date_choose, TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日"));
				showNextDayBtn();
				getdata();
				break;
			case R.id.tv_date_from:
				intent.setClass(mActivity, ChooseDateActivity.class);
				String tmp_date_str_from = TimeUtil.getStringFromDate(panel_dateFrom==null?(new Date()):panel_dateFrom, ChooseDateActivity.FORMAT_DATE);
				intent.putExtra(ChooseDateActivity.PARA_SELECTED_DATE, tmp_date_str_from);
				intent.putExtra("chooseType", ChooseDateActivity.CHOOSE_BEFORE_TODAY);
				intent.putExtra("toDate", TimeUtil.getStringFromDate(panel_dateTo, ChooseDateActivity.FORMAT_DATE));
				startActivityForResult(intent, REQUEST_CODE_QUERYDATE_FROM);
				break;
			case R.id.tv_date_to:
				intent.setClass(mActivity, ChooseDateActivity.class);
				String tmp_date_str_to = TimeUtil.getStringFromDate(panel_dateTo==null?(new Date()):panel_dateTo, ChooseDateActivity.FORMAT_DATE);
				intent.putExtra(ChooseDateActivity.PARA_SELECTED_DATE, tmp_date_str_to);
				intent.putExtra("chooseType", ChooseDateActivity.CHOOSE_BEFORE_TODAY);
				intent.putExtra("fromDate", TimeUtil.getStringFromDate(panel_dateFrom, ChooseDateActivity.FORMAT_DATE));
				startActivityForResult(intent, REQUEST_CODE_QUERYDATE_TO);
				break;
			case R.id.btn_filter_confirm:
				if(dialog_panel!=null){dialog_panel.dismiss();}
				query_dateFrom = panel_dateFrom;
				query_dateTo = panel_dateTo;
				panel_dateFrom = query_dateFrom;
				panel_dateTo = query_dateTo;
				String from = TimeUtil.getStringFromDate(query_dateFrom, "yyyy年MM月dd日");
				String to = TimeUtil.getStringFromDate(query_dateTo, "yyyy年MM月dd日");
				boolean checkshow = from.equals(to);
				showView(R.id.ly_date_chooser, checkshow?true:false);
				showView(R.id.ly_date_show, checkshow?false:true);
				setTvText(R.id.tv_date_choose, from);
				setTvText(R.id.tv_date_show, from+" - "+to);
				showNextDayBtn();
				getdata();
				break;
			default:break;
		}
		super.onClick(v);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Intent intent = new Intent();
		intent.setClass(mActivity, TradeDetailActivity.class);
		intent.putExtra("orderDetail", datas.get(position));
		startActivity(intent);
	}
	
	private void showPanel() {

		dialog_panel = new AlertDialog.Builder(mActivity).create();
		dialog_panel.show();
		Window window = dialog_panel.getWindow();
		
		WindowManager.LayoutParams lp =window.getAttributes();
		lp.x=0;
		lp.y=150;
		
		// 设置布局
		window.setContentView(R.layout.dialog_receipt_info_filter);
		// 设置宽高
		window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		// 设置弹出的动画效果
		window.setGravity(Gravity.TOP);
		
		tv_date_from = (TextView) window.findViewById(R.id.tv_date_from);
		tv_date_to = (TextView) window.findViewById(R.id.tv_date_to);
		tv_date_from.setOnClickListener(this);
		tv_date_to.setOnClickListener(this);
		
		window.findViewById(R.id.btn_filter_confirm).setOnClickListener(this);
		
		gv_option_paytype = (GridView) window.findViewById(R.id.gv_option_paytype);
		adapter_paytype = new ChooseOptionAdapter(mActivity, paytype_values, paytype_marks);
		gv_option_paytype.setAdapter(adapter_paytype);
		gv_option_paytype.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				adapterNotify(TYPE_PAY_OPTION, arg2);
			}
		});
		
		reflashPanel();
		
	}
	
	private void adapterNotify(int adapterType, int index){
		if(adapterType==TYPE_PAY_OPTION){
			payType = EnumConsts.SqbPayChannel.getTypeByName(paytype_values.get(index));
			reflashPanel();
		}
	}
	
	private void reflashPanel(){
		tv_date_from.setText(TimeUtil.getStringFromDate(panel_dateFrom, "yyyy年MM月dd日"));
		tv_date_to.setText(TimeUtil.getStringFromDate(panel_dateTo, "yyyy年MM月dd日"));
		int size2 = paytype_values.size();
		for (int i = 0; i < size2; i++) {
			int type_tmp = EnumConsts.SqbPayChannel.getTypeByName(paytype_values.get(i));
			paytype_marks.set(i, type_tmp==payType?true:false);
		}
		adapter_paytype.notifyDataSetChanged();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode!=RESULT_OK)return;
		switch (requestCode) {
			case REQUEST_CODE_QUERYDATE_FROM:
				String a = data.getStringExtra(ChooseDateActivity.PARA_SELECTED_DATE);
				panel_dateFrom = TimeUtil.getDateFromString(a, ChooseDateActivity.FORMAT_DATE);
				tv_date_from.setText(TimeUtil.getStringFromDate(panel_dateFrom, "yyyy年MM月dd日"));
				break;
			case REQUEST_CODE_QUERYDATE_TO:
				String b = data.getStringExtra(ChooseDateActivity.PARA_SELECTED_DATE);
				panel_dateTo = TimeUtil.getDateFromString(b, ChooseDateActivity.FORMAT_DATE);
				tv_date_to.setText(TimeUtil.getStringFromDate(panel_dateTo, "yyyy年MM月dd日"));
				break;
			default: break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void showNextDayBtn(){
		String a = TimeUtil.getStringFromDate(query_dateFrom, "yyyyMMdd");
		String b = TimeUtil.getStringFromDate(query_dateTo, "yyyyMMdd");
		String c = TimeUtil.getCurrentDateString("yyyyMMdd");
		showView(R.id.ly_next, !(a.equals(b)&&b.equals(c)));
	}
	
}
